﻿if (typeof (Sou) == "undefined")
    Sou = {};
(function () {
    var ie = !!(window.attachEvent && !window.opera);
    var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
    var fn = [];
    var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
    var d = document;
    d.ready = function (f) {
        if (!ie && !wk && d.addEventListener)
            return d.addEventListener('DOMContentLoaded', f, false);
        if (fn.push(f) > 1) return;
        if (ie)
            (function () {
                try { d.documentElement.doScroll('left'); run(); }
                catch (err) { setTimeout(arguments.callee, 0); }
            })();
        else if (wk)
            var t = setInterval(function () {
                if (/^(loaded|complete)$/.test(d.readyState))
                    clearInterval(t), run();
            }, 0);
    };
})();
if (typeof (Sou.Ajax) == "undefined")
    Sou.Ajax = {};
Sou.Ajax.lastScript;
Sou.Ajax.h = document.getElementsByTagName("head")[0];
Sou.Ajax.loadScript = function (url) {
    var f = document.createElement("script");
    var d = new Date().getTime();
    f.type = "text/javascript";
    f.id = d;
    f.src = url + '&d=' + d;
    Sou.Ajax.h.appendChild(f);
    if (Sou.Ajax.lastScript && Sou.Ajax.g(Sou.Ajax.lastScript))
        Sou.Ajax.g(Sou.Ajax.lastScript).parentNode.removeChild(Sou.Ajax.g(Sou.Ajax.lastScript));
    Sou.Ajax.lastScript = d;
}
Sou.Ajax.loadScript = function (url, isRemove) {
    var f = document.createElement("script");
    var d = new Date().getTime();
    f.type = "text/javascript";
    f.id = d;
    f.src = url + '&d=' + d;
    Sou.Ajax.h.appendChild(f);
    if (Sou.Ajax.lastScript && Sou.Ajax.g(Sou.Ajax.lastScript) && isRemove)
        Sou.Ajax.g(Sou.Ajax.lastScript).parentNode.removeChild(Sou.Ajax.g(Sou.Ajax.lastScript));
    Sou.Ajax.lastScript = d;
}
Sou.Ajax.g = function (x) {
    return document.getElementById(x)
};
if (typeof (Sou.Autocomplate) == "undefined")
    Sou.Autocomplate = {};
Sou.Autocomplate.tipNum = 0;
Sou.Autocomplate.AddAutocomplate = function (obj) {
    var keyword = obj.value;
    var tip = document.getElementById("autocomplateTip");
    var timeOut;
    var isIE = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
    function getData(e) {
        e = e || window.event;
        if (e.keyCode != 38 && e.keyCode != 40) {
            tipNum = 0;
            try {
                Sou.Ajax.loadScript("http://sou.autohome.com.cn/Controls/StraightQueryHandler.ashx?q=" + escape(obj.value), true);
            } catch (e) {
            }
        }
    }
    function move(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            if (isIE) {
                e.preventDefault();
            }
            if (document.getElementById("pvareaid") != null)
                document.getElementById("pvareaid").value = "100918";
            document.getElementById("souform").submit();
        }
        if (e.keyCode == 38 || e.keyCode == 40) {
            if (tip.style.display != "none") {
                var max = tip.getElementsByTagName("li").length;
                if (e.keyCode == 38) {
                    tipNum--;
                    if (tipNum <= 0)
                        tipNum = max;
                }
                if (e.keyCode == 40) {
                    tipNum++;
                    if (tipNum > max)
                        tipNum = 1;
                }
                var current = tip.getElementsByTagName("a")[tipNum - 1];
                for (var i = 0; i < max; i++) {
                    tip.getElementsByTagName("a")[i].style.backgroundColor = "";
                }
                current.style.backgroundColor = "#e7f0f9"
                obj.value = current.title;
            }
        }
    }
    obj.onkeyup = getData;
    obj.onkeydown = move;
};

Sou.Autocomplate.summitform = function (keyword, type, seriesid) {
    document.getElementById("souform").getElementsByTagName("input")[0].value = keyword;
    if (document.getElementById("pvareaid") != null)
        document.getElementById("pvareaid").value = "100917";
    if (type == 1) {
        window.open("http://sou.autohome.com.cn/TipWordSouLog.aspx?word=" + keyword + "&seriesid=" + seriesid + "&type=0");
    } else {
        document.getElementById("souform").submit();
    }
}
Sou.Autocomplate.bindAutocomplate = function (json) {
    var tip = document.getElementById("autocomplateTip");
    if (json.length > 0) {
        var arrKey = json.split(',');
        var strHtml = "<ul class=\"sct_1\">";
        var strSeriesHtml = "<div class=\"sct_tt\"><em class=\"ico_arrow\"></em>点击直达</div><ul class=\"sct_2\">";
        var strSeries = "";
        for (var i = 0; i < arrKey.length; i++) {
            var id = "div" + (i + 1);
            var arrSeriesKey = arrKey[i].toString().split(':');
            if (arrSeriesKey.length > 0 && arrSeriesKey[2] != null) {
                var pricestr = "暂无报价";
                if (arrSeriesKey[2] != 0 || arrSeriesKey[3] != 0) {
                    pricestr = arrSeriesKey[2] + "-" + arrSeriesKey[3] + "万";
                }
                strSeries += "<li><a href=\"javascript:void(0);\" target=\"_self\" onclick=\"Sou.Autocomplate.summitform('" + arrSeriesKey[0] + "',1," + arrSeriesKey[1] + ")\" title=\"" + arrSeriesKey[0] + "\"><span class=\"sct_b\">" + arrSeriesKey[0] + "</span><em class=\"ico_arrow\"></em><span class=\"sct_p\">" + pricestr + "</span></a></li>"
            } else {
                var keyword = document.getElementById("q").value;
                var show = arrKey[i].indexOf(keyword) > -1 ? arrKey[i].substring(0, arrKey[i].indexOf(keyword) + keyword.length) + "<strong>" + arrKey[i].substring(arrKey[i].indexOf(keyword) + keyword.length) + "</strong>" : arrKey[i];
                strHtml += "<li><a href=\"javascript:void(0);\" target=\"_self\" onclick=\"Sou.Autocomplate.summitform('" + arrKey[i] + "',0,0)\" title=\"" + arrKey[i] + "\">" + show + "</a></li>";
            }
        }
        strHtml += "</ul>";
        strSeries += "</ul>";
        if (strSeries != "</ul>") {
            strSeriesHtml += strSeries;
        } else {
            strSeriesHtml = "";
        }
        strHtml += strSeriesHtml;
        tip.innerHTML = strHtml;
        tip.style.display = "block";
    }
    else {
        tip.style.display = "none";
    }
}
document.onclick = function (e) {
    e = e || window.event;
    var dom = e.srcElement || e.target;
    var objword = document.getElementById("autocomplateTip");
    if (objword != null) {
        if (dom.id != "autocomplateTip" && document.getElementById("autocomplateTip").style.display == "block") {
            document.getElementById("autocomplateTip").style.display = "none";
        }
    }
};
document.ready(function () {
    Sou.Autocomplate.AddAutocomplate(document.getElementById("souform").getElementsByTagName("input")[0]); 
});